package com.pd.backend.service.impl;

import com.pd.backend.entity.PdAddressBook;
import com.pd.backend.dao.PdAddressBookDao;
import com.pd.backend.service.PdAddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 地址簿(PdAddressBook)表服务实现类
 *
 * @author GuoXinChao.126@gmail.com
 * @since 2023-02-27 17:14:31
 */
@Service("pdAddressBookService")
public class PdAddressBookServiceImpl implements PdAddressBookService {
    @Resource
    private PdAddressBookDao pdAddressBookDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PdAddressBook queryById(String id) {
        return this.pdAddressBookDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param pdAddressBook 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<PdAddressBook> queryByPage(PdAddressBook pdAddressBook, PageRequest pageRequest) {
        long total = this.pdAddressBookDao.count(pdAddressBook);
        return new PageImpl<>(this.pdAddressBookDao.queryAllByLimit(pdAddressBook, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param pdAddressBook 实例对象
     * @return 实例对象
     */
    @Override
    public PdAddressBook insert(PdAddressBook pdAddressBook) {
        this.pdAddressBookDao.insert(pdAddressBook);
        return pdAddressBook;
    }

    /**
     * 修改数据
     *
     * @param pdAddressBook 实例对象
     * @return 实例对象
     */
    @Override
    public PdAddressBook update(PdAddressBook pdAddressBook) {
        this.pdAddressBookDao.update(pdAddressBook);
        return this.queryById(pdAddressBook.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.pdAddressBookDao.deleteById(id) > 0;
    }
}
